package com.gbs.info2.tools;

import java.io.IOException;
import java.net.SocketException;
import java.util.ArrayList;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import android.content.Context;
import com.gbs.info2.obj.VehicleMobile;

public class WShelper {

	private static final String SOAP_ACTION = "getAllVehicleMobile";
	private static final String METHOD_NAME = "getAllVehicleMobile";
	private static final String NAMESPACE = "urn:GBSi2wsVi";
	ConfigFileHelper configFileHelper;

	public WShelper(Context context) {
		configFileHelper = new ConfigFileHelper(context);
	}
	public ArrayList<VehicleMobile> getVehicleList() {
		ArrayList<VehicleMobile> vehicleMobiles = new ArrayList<VehicleMobile>();
		try {
			// Create SOAP request
			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			envelope.setOutputSoapObject(request);
			HttpTransportSE androidHttpTransport = new HttpTransportSE(configFileHelper.readLine(2));
			androidHttpTransport.call(SOAP_ACTION, envelope);

			SoapObject soapobject = (SoapObject) envelope.getResponse(); // get
																			// response
			int totalCount = soapobject.getPropertyCount();
			for (int detailCount = 0; detailCount < totalCount; detailCount++) {
				SoapObject pojoSoap = (SoapObject) soapobject.getProperty(detailCount);
				VehicleMobile vehicleMobile = new VehicleMobile();
				vehicleMobile.setTitle(pojoSoap.getProperty("title").toString());
				vehicleMobile.setEcv(pojoSoap.getProperty("ecv").toString());
				vehicleMobile.setFuelType(pojoSoap.getProperty("fuelType").toString());
				vehicleMobile.setLocationType(pojoSoap.getProperty("locationType").toString());
				try {
					vehicleMobile.setPin(pojoSoap.getProperty("pin").toString());
				} catch (Exception e) {
					vehicleMobile.setPin("");
				}
				try {
					vehicleMobile.setSpecification(pojoSoap.getProperty("specification").toString());
				} catch (Exception e) {
					vehicleMobile.setSpecification("");
				}
				try {
					vehicleMobile.setFailures(pojoSoap.getProperty("failures").toString());
				} catch (Exception e) {
					vehicleMobile.setFailures("");
				}
				vehicleMobile.setCentralLock(Integer.parseInt(pojoSoap.getProperty("centralLock").toString()));
				vehicleMobile.setRemoteControl(Integer.parseInt(pojoSoap.getProperty("remoteControl").toString()));
				vehicleMobile.setAlarm(Integer.parseInt(pojoSoap.getProperty("alarm").toString()));
				vehicleMobile.setSpareKeys(Integer.parseInt(pojoSoap.getProperty("spareKeys").toString()));
				vehicleMobile.setVehiclePhoto(pojoSoap.getProperty("vehiclePhoto").toString().getBytes());
				vehicleMobiles.add(vehicleMobile);
			}
		} catch (SocketException se) {
			return null;
		} catch (XmlPullParserException e) {
			// TODO: handle exception
			//exp pri chybnej url adrese
			return null;
			//e.printStackTrace();
		} catch (IOException e) {
			return null;
		}
		return vehicleMobiles;
	}

}
